import React, {Component} from 'react';
import './index.css'
import {nanoid} from 'nanoid'

class Index extends Component {

  handlerKeyUp = (event) => {
    const {keyCode, target} = event;
    if (keyCode !== 13) {
      return
    }

    if (target.value.trim() === '') {
      alert('输入不能为空')
      return
    }

    const todoObj = {name: target.value, id: nanoid(), checked: false};
    this.props.addTodo(todoObj)
    target.value = '';
  }

  render() {
    return (
        <div className="todo-header">
          <input type="text" onKeyUp={this.handlerKeyUp}
                 placeholder="请输入你的任务名称，按回车键确认"/>
        </div>
    );
  }
}

export default Index;